package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShopCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/shoppingCart")
@RestController
@Slf4j
@Api(tags = "c端购物车")
public class ShopCarController {

    @Autowired
    private ShopCarService shopCarService;

    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车的信息:{}",shoppingCartDTO);
        shopCarService.addShoppingCar(shoppingCartDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public Result<List<ShoppingCart>> showShoppingCar(){
        log.info("查询购物车信息");
        List<ShoppingCart> list = shopCarService.showShoppingCar();
        return Result.success(list);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result cleanShoppingCar(){
        log.info("清空购物车");
        shopCarService.cleanShoppingCar();
        return Result.success();
    }

    @PostMapping("/sub")
    @ApiOperation("删除一个商品")
    public Result subOneProduct(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除一个商品:{}",shoppingCartDTO);
        shopCarService.subShoppingCar(shoppingCartDTO);
        return Result.success();
    }
}
